$(function(){

    CKEDITOR.disableAutoInline = true;

//    $('body').append($('<a id="enable-content-editable-mode" style="display: block; width: 20px; height: 20px; text-decoration: none; padding: 15px; position: absolute; right: 0px; top: 0px; font-size: 15px; background: #ccc;">CE</a>'))


    $('#enable-content-editable-mode').on('click', function(){

        if(!$(this).data('enabled'))
        {
            $(this).css({background: '#000099'});
            ContentEditableMode.init();
        }
        else
        {
//            $(this).data('enabled', false);
            $(this).css({background: '#ccc'});
            ContentEditableMode.destroy();
        }

        $(this).data('enabled', !$(this).data('enabled'));
    });

});

ContentEditableMode = {

    init : function(){
        $('.content-editable.gategories li a').attr('contenteditable', true);
        $('.content-editable').attr('contenteditable', true);

        $('[contenteditable="true"]').on('blur', ContentEditableMode.changeHandler);

        CKEDITOR.inlineAll();
//        console.log(editor);
        CKEDITOR.on( 'instanceCreated', function( event ) {
            var editor = event.editor,
                element = editor.element;

            if ( element.is( 'h1', 'h2', 'h3' ) || element.getAttribute( 'id' ) == 'taglist' ) {
                editor.on( 'configLoaded', function() {

                    // Remove unnecessary plugins to make the editor simpler.
                    editor.config.removePlugins = 'colorbutton,find,flash,font,' +
                        'forms,iframe,image,newpage,removeformat,' +
                        'smiley,specialchar,stylescombo,templates';

                    // Rearrange the layout of the toolbar.
                    editor.config.toolbarGroups = [
                        { name: 'editing',		groups: [ 'basicstyles', 'links' ] },
                        { name: 'undo' },
                        { name: 'clipboard',	groups: [ 'selection', 'clipboard' ] },
                        { name: 'about' }
                    ];
                });
            }
        });
    },

    changeHandler : function(e) {
        var dataUrl = $(this).attr('data-url');
        var field = dataUrl.replace(/.*field\//i, '');

        var request = {};

        if(field.length > 0)
            request[field] = $(this).text();
        else
            request.title = $(this).text();

        $.post(dataUrl, request, function(response){
            console.log(response);
        }, 'json');
    },

    destroy : function(){
        if(CKEDITOR){
            if(CKEDITOR.instances){
                for(var i in CKEDITOR.instances)
                    CKEDITOR.instances[i].destroy();
            }
        }

        $('[contenteditable="true"]').off('blur', ContentEditableMode.changeHandler);
        $('.content-editable.gategories li a').attr('contenteditable', false);
        $('.content-editable').attr('contenteditable', false);
    }

}